import {resetForm} from 'plugins/utils.js'

export function reset (state) {
  state.fields = {file_replace: false, host: [], service: [], javaopt: null, pubcmd: null, docker_opt: null, docker_cmd: null}
  state.showBuildParam = false
  state.showVMSelect = false
  state.showDockerSelect = false
  state.showPath = false
  resetForm(state.msg)
}

export function saveSuccess (state) {
  this.$router.push('/listproject')
  state.loading.hide()
}

export function saveFail (state, payload) {
  for (var name in payload) {
    state.msg[name] = payload[name][0]
  }
  state.loading.hide()
}

export function loadSelect (state) {
  state.env = this._vm.$data.$$state.listProject.env
  // state.parent = this._vm.$data.$$state.listProject.parent
  state.project_nature = this._vm.$data.$$state.listProject.project_nature
  state.projectType = this._vm.$data.$$state.listProject.projectType
  state.status = this._vm.$data.$$state.listProject.status
  state.publish_type = this._vm.$data.$$state.listProject.publish_type
  state.owner = this._vm.$data.$$state.listProject.owner
  state.build_param = this._vm.$data.$$state.listProject.build_param
  state.pub_version = this._vm.$data.$$state.listProject.pub_version
  state.detection = this._vm.$data.$$state.listProject.detection
}

export function selectService (state) {
  state.serviceModal = true
}

export function selectHost (state) {
  state.hostModal = true
}

export function loadSucess (state, payload) {
  state.fields = payload
  state.selectedHost = payload.hosts
  state.selectedService = payload.services
}

export function changePublishType (state, payload) {
  return new Promise((resolve, reject) => {
    if (payload) {
      state.javaopt = {items: [], value: null}
      state.pubcmd = {items: [], value: null}
      state.fields.pubcmd = null
      state.fields.javaopt = null
    }
    state.showVMSelect = state.fields.publish_type === 1
    resolve()
  }).catch((error) => {
    console.log(error)
  })
}

export function changeEnv (state, payload) {
  return new Promise((resolve, reject) => {
    if (payload) {
      state.fields.host = []
      state.selectedHost = []
      state.fields.service = []
      state.selectedService = []
      state.fields.docker_cmd = null
      state.dockercmd = {items: [], value: null}
      state.pubcmd = {items: [], value: null}
      state.fields.pubcmd = null
    }
    state.showVMSelect = state.fields.publish_type === 1
    resolve()
  }).catch((error) => {
    console.log(error)
  })
}

export function changePubVersion (state, payload) {
  return new Promise((resolve, reject) => {
    switch (state.fields.pub_version) {
      case 0:
        state.showBuildParam = false
        state.showDockerSelect = false
        state.fields.docker_opt = ''
        state.fields.docker_cmd = ''
        break
      case 1:
        state.showBuildParam = true
        state.showDockerSelect = false
        state.fields.docker_opt = ''
        state.fields.docker_cmd = ''
        break
      case 2:
        state.showBuildParam = true
        state.showDockerSelect = true
        if (payload) {
          state.fields.docker_opt = null
          state.dockeropt = {items: [], value: null}
        }
    }
    resolve()
  }).catch((error) => {
    console.log(error)
  })
}

export function changeFileReplace (state) {
  return new Promise((resolve, reject) => {
    if (state.fields.file_replace) {
      state.showPath = true
    } else {
      state.showPath = false
      state.fields.relative_conf_path = ''
      state.fields.absolute_conf_path = ''
    }
    resolve()
  }).catch((error) => {
    console.log(error)
  })
}

export function watchSelectedHost (state) {
  let array = []
  for (let i in state.selectedHost) {
    array[i] = state.selectedHost[i].value
  }
  state.fields.host = array
}

export function watchSelectedService (state) {
  let array = []
  for (let i in state.selectedService) {
    array[i] = state.selectedService[i].value
  }
  state.fields.service = array
}

export function projectJavaoptSuccess (state, payload) {
  let res = JSON.parse(payload)
  state.javaopt.items = res.data
}

export function projectGetPubcmdSuccess (state, payload) {
  let res = JSON.parse(payload)
  state.pubcmd.items = res.data
}

export function projectGetHostSuccess (state, payload) {
  let res = JSON.parse(payload)
  for (let i in res.data) {
    res.data[i].avatar = '/statics/' + res.data[i].avatar + '.png'
  }
  state.hostData = res.data
}

export function projectGetServiceSuccess (state, payload) {
  let res = JSON.parse(payload)
  state.serviceData = res.data
}

export function getDockerCmdSuccess (state, payload) {
  let res = JSON.parse(payload)
  state.dockercmd.items = res.data
}

export function getDockerOptSuccess (state, payload) {
  let res = JSON.parse(payload)
  state.dockeropt.items = res.data
}

export function loadProjectParentSuccess (state, payload) {
  let res = JSON.parse(payload)
  state.parent.items = res.data
}
